package com.demo.window.insets

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.demo.window.insets.databinding.ActivityMainBinding
import com.liulu.tools.appScreenSizePoint
import com.liulu.tools.screenSizePoint
import com.liulu.tools.screenSizeRect
import com.liulu.window.insets.immerse

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mBinding: ActivityMainBinding
    private var mWindowInsetsController: WindowInsetsControllerCompat? = null
    private var mWindowInsets: WindowInsetsCompat? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        Log.d("tag_main", "onCreate")
        initView()
    }

    private fun initView() {
        mBinding.btn0.setOnClickListener(this)
        mBinding.btn1.setOnClickListener(this)
        mBinding.btn2.setOnClickListener(this)
        mBinding.btn3.setOnClickListener(this)
        mBinding.btn4.setOnClickListener(this)
        mBinding.btn5.setOnClickListener(this)
        mBinding.btn6.setOnClickListener(this)
        mBinding.btn7.setOnClickListener(this)
        mBinding.btn8.setOnClickListener(this)
        mBinding.btn9.setOnClickListener(this)
        mBinding.btn10.setOnClickListener(this)
        mBinding.btn11.setOnClickListener(this)
        mBinding.btn12.setOnClickListener(this)
        mBinding.btn13.setOnClickListener(this)
        mBinding.btn14.setOnClickListener(this)
        mBinding.btn15.setOnClickListener(this)
        mBinding.btn16.setOnClickListener(this)
        mBinding.btn17.setOnClickListener(this)
        mBinding.btn18.setOnClickListener(this)
        mBinding.btn19.setOnClickListener(this)
        mBinding.btn20.setOnClickListener(this)
        mBinding.btn21.setOnClickListener(this)
        mBinding.btn22.setOnClickListener(this)
        window?.let {
            mWindowInsetsController = WindowCompat.getInsetsController(it, mBinding.root)
        }
        immerse(listener = { _, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            Log.d(
                "tag_main",
                "onApplyWindowInsets: statusBarHeight: ${systemBars.top}，navBarHeight: ${systemBars.bottom}"
            )
            mWindowInsets = insets
            insets
        })
        mBinding.root.post {
            ViewCompat.getRootWindowInsets(window.decorView)?.let { insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                val top = systemBars.top
                val bottom = systemBars.bottom
                Log.d(
                    "tag_main",
                    "RootWindowInsets: statusBarHeight: $top，navBarHeight: $bottom"
                )
                val lp = mBinding.btn0.layoutParams
                if (lp is ConstraintLayout.LayoutParams) {
                    lp.topMargin = top
                    mBinding.btn0.layoutParams = lp
                }
            } ?: let {
                Log.d("tag_main", "RootWindowInsets: null")
            }
        }

        //statusBarHeight: 56，navBarHeight: 84  1544-56-84=1404
        //Physical size: 720x1544
        //screenSizePoint: 720x1544
        //screenSizeRect: 720x1544
        //appScreenSizePoint: 720x1407

        //statusBarHeight: 89，navBarHeight: 130  2248-89-130=2029
        //Physical size: 1080x2248
        //screenSizePoint: 1080x2248
        //screenSizeRect: 1080x2248
        //appScreenSizePoint: 1080x2029

        //statusBarHeight: 146，navBarHeight: 0  3200-146=3054
        //Physical size: 1440x3200
        //screenSizePoint: 1440x3200
        //screenSizeRect: 1440x3200
        //appScreenSizePoint: 1440x3054

        val screenWidth1 = screenSizePoint.x
        val screenHeight1 = screenSizePoint.y
        val screenWidth2 = screenSizeRect.width()
        val screenHeight2 = screenSizeRect.height()
        val screenWidth3 = appScreenSizePoint.x
        val screenHeight3 = appScreenSizePoint.y
        Log.d("tag_main", "screenSizePoint: ${screenWidth1}x${screenHeight1}")
        Log.d("tag_main", "screenSizeRect: ${screenWidth2}x${screenHeight2}")
        Log.d("tag_main", "appScreenSizePoint: ${screenWidth3}x${screenHeight3}")
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn0 -> btn0()
            R.id.btn1 -> btn1()
            R.id.btn2 -> btn2()
            R.id.btn3 -> btn3()
            R.id.btn4 -> btn4()
            R.id.btn5 -> btn5()
            R.id.btn6 -> btn6()
            R.id.btn7 -> btn7()
            R.id.btn8 -> btn8()
            R.id.btn9 -> btn9()
            R.id.btn10 -> btn10()
            R.id.btn11 -> btn11()
            R.id.btn12 -> btn12()
            R.id.btn13 -> btn13()
            R.id.btn14 -> btn14()
            R.id.btn15 -> btn15()
            R.id.btn16 -> btn16()
            R.id.btn17 -> btn17()
            R.id.btn18 -> btn18()
            R.id.btn19 -> btn19()
            R.id.btn20 -> btn20()
            R.id.btn21 -> btn21()
            R.id.btn22 -> btn22()
        }
    }

    private fun btn0() {
        startActivity(Intent(this, Main2Activity::class.java))
    }

    private fun btn1() {
        mWindowInsets?.let { insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            mBinding.root.setPadding(
                systemBars.left,
                systemBars.top,
                systemBars.right,
                systemBars.bottom
            )
        }
    }

    private fun btn2() {
        mWindowInsets?.let { insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            mBinding.root.setPadding(
                0,
                0,
                0,
                0
            )
        }
    }

    private fun btn3() {
        mWindowInsets?.let { insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            mBinding.root.setPadding(
                0,
                0,
                0,
                systemBars.bottom
            )
        }
    }

    private fun btn4() {
        mWindowInsets?.let { insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            mBinding.root.setPadding(
                0,
                systemBars.top,
                0,
                0
            )
        }
    }

    private fun btn5() {
        //浅色模式(light)，白底黑字
        //深色模式(dark)，黑底白字
        mWindowInsetsController?.isAppearanceLightStatusBars = true
    }

    private fun btn6() {
        mWindowInsetsController?.isAppearanceLightStatusBars = false
    }

    private fun btn7() {
        mWindowInsetsController?.isAppearanceLightNavigationBars = true
    }

    private fun btn8() {
        mWindowInsetsController?.isAppearanceLightNavigationBars = false
    }

    private fun btn9() {
        mWindowInsetsController?.show(WindowInsetsCompat.Type.statusBars())
    }

    private fun btn10() {
        mWindowInsetsController?.hide(WindowInsetsCompat.Type.statusBars())
    }

    private fun btn11() {
        mWindowInsetsController?.show(WindowInsetsCompat.Type.navigationBars())
    }

    private fun btn12() {
        mWindowInsetsController?.hide(WindowInsetsCompat.Type.navigationBars())
        //导航栏隐藏时手势操作
        mWindowInsetsController?.systemBarsBehavior =
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        //systemBarsBehavior有三个值：
        //BEHAVIOR_SHOW_BARS_BY_SWIPE
        //BEHAVIOR_SHOW_BARS_BY_TOUCH
        //BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        //目前可能跟部分手机手势有冲突
    }

    private fun btn13() {
        mWindowInsetsController?.show(WindowInsetsCompat.Type.systemBars())
    }

    private fun btn14() {
        mWindowInsetsController?.hide(WindowInsetsCompat.Type.systemBars())
    }

    private fun btn15() {
        mWindowInsetsController?.show(WindowInsetsCompat.Type.ime())
    }

    private fun btn16() {
        mWindowInsetsController?.hide(WindowInsetsCompat.Type.ime())
    }

    private fun btn17() {
        //非沉浸式有效
        window?.statusBarColor = Color.TRANSPARENT
    }

    private fun btn18() {
        window?.statusBarColor = Color.BLACK
    }

    private fun btn19() {
        window?.statusBarColor = Color.WHITE
    }

    private fun btn20() {
        window?.let {
            it.navigationBarColor = Color.TRANSPARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                it.navigationBarDividerColor = Color.TRANSPARENT
            }
        }
    }

    private fun btn21() {
        window?.let {
            it.navigationBarColor = Color.BLACK
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                it.navigationBarDividerColor = Color.BLACK
            }
        }
    }

    private fun btn22() {
        window?.let {
            it.navigationBarColor = Color.WHITE
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                it.navigationBarDividerColor = Color.WHITE
            }
        }
    }
}